package com.auxgroup.smarthome.enterprise.service;

import com.auxgroup.smarthome.enterprise.vo.UserAnalyzeVo;
import com.auxgroup.smarthome.enterpriseapi.EnterpriseApi;
import com.auxgroup.smarthome.enterpriseapi.locations.CityCountBean;
import com.auxgroup.smarthome.enterpriseapi.locations.RegionCountBean;
import com.auxgroup.smarthome.enterpriseapi.requestbody.BigDataHistoryBody;
import com.auxgroup.smarthome.enterpriseapi.responsebody.UserNewaddReport;
import com.auxgroup.smarthome.enterpriseapi.responsebody.UserTotalDetails;
import com.auxgroup.smarthome.web.ApiResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Iterator;
import java.util.List;

/**
 * 用户报表
 * Created by Toby Lai on 2017/4/24.
 */
@Service
public class UsersReportService {

    @Autowired
    private ProductService productService;

    /**
     * 用户分析报表
     * @param pk
     * @param gid
     * @return
     */
    public ApiResponse<UserAnalyzeVo> getUserAnalyze(String pk, String gid) {
        UserTotalDetails userTotalDetails = EnterpriseApi.UserReport.getUserTotal(productService.getToken(pk), pk,gid);
        UserAnalyzeVo userAnalyzeVo = new UserAnalyzeVo();
        userAnalyzeVo.setUserTotal(userTotalDetails.getTotal());
        userAnalyzeVo.setActiveTotal(0);
        userAnalyzeVo.setActiveTotalCompare(0);
        userAnalyzeVo.setUserActiveRate(0);
        userAnalyzeVo.setUserActiveRateCompare(0);
        userAnalyzeVo.setUserRateOfRise(0);
        userAnalyzeVo.setUserRateOfRiseCompare(0);
        userAnalyzeVo.setUserTotalCompare(0);

        return ApiResponse.ok(userAnalyzeVo);
    }

    /**
     * 用户总数报表（实时）
     * @param pk 必须
     * @param gid 可选
     * @return
     */
    public UserTotalDetails getUserTotal(String pk, String ... gid){
        return EnterpriseApi.UserReport.getUserTotal(productService.getToken(pk), pk,gid);
    }

    /**
     * 新增用户趋势
     * @param pk
     * @param body
     * @return
     */
    public UserNewaddReport getUserNewAddTrend(String pk, BigDataHistoryBody body){
        UserNewaddReport userNewAdd = EnterpriseApi.UserReport.getUserNewAdd(productService.getToken(pk), pk, body);
        return userNewAdd;
    }

    /**
     * 活跃用户趋势
     * @param pk
     * @param body
     * @return
     */
    public UserNewaddReport getUserLivenessTrend(String pk, BigDataHistoryBody body) {
        UserNewaddReport userLivenessTrend = EnterpriseApi.UserReport.getUserNewAdd(productService.getToken(pk), pk, body);
        if (StringUtils.isNotBlank(userLivenessTrend.getError_message())){
            return userLivenessTrend;
        }
        //假数据
        if (pk.compareToIgnoreCase("7af526e214f64899928d443587909f85")==0){
            List<UserNewaddReport.HistoryList> data = userLivenessTrend.getData();
            UserNewaddReport.HistoryList historyList = data.get(data.size()-1);historyList.setCount(4);
            data.add(data.size()-1,historyList);
            UserNewaddReport.HistoryList historyList1 = data.get(data.size()-2);historyList.setCount(5);
            data.add(data.size()-2,historyList1);
        }
        return userLivenessTrend;
    }

    /**
     * 用户总数趋势
     * @param pk
     * @param body
     * @return
     */
    public UserNewaddReport getUserTotalTrend(String pk, BigDataHistoryBody body) {
        UserTotalDetails userTotal = getUserTotal(pk);
        UserNewaddReport userTotalTrend = EnterpriseApi.UserReport.getUserNewAdd(productService.getToken(pk), pk, body);
        if (StringUtils.isNotBlank(userTotalTrend.getError_message())){
            return userTotalTrend;
        }
        //假数据
        if (pk.compareToIgnoreCase("7af526e214f64899928d443587909f85")==0){
            List<UserNewaddReport.HistoryList> data = userTotalTrend.getData();
            for (int i = 0; i < data.size() ; i++) {
                UserNewaddReport.HistoryList historyList = data.get(i);
                if (data.size()==7){
                    if (i<3){
                        historyList.setCount(14);
                    }
                    if (i>=3 && i<=5){
                        historyList.setCount(15);
                    }
                    if (i==data.size()-1){
                        historyList.setCount(userTotal.getTotal());
                    }
                }
                if (data.size() > 25 && data.size() <= 30){
                    if (i<9){
                        historyList.setCount(12);
                    }
                    if (i>=9 && i <=15){
                        historyList.setCount(13);
                    }
                    if (i>15 && i < data.size()-1){
                        historyList.setCount(15);
                    }
                    if (i==data.size()-1){
                        historyList.setCount(userTotal.getTotal());
                    }
                }

            }
        }
        return userTotalTrend;
    }

    @Autowired
    ProductStatisticsService productStatisticsService;

    public ApiResponse<List<RegionCountBean>> getUserLocationDistribution(@PathVariable("productKey") String pk){
        ApiResponse<List<RegionCountBean>> chinaRegionList = productStatisticsService.getChinaRegionList(pk, null);
        if (pk.compareToIgnoreCase("7af526e214f64899928d443587909f85")==0) {
            List<RegionCountBean> data = chinaRegionList.getData();
            for(Iterator<RegionCountBean> iterator = data.iterator();iterator.hasNext();){
                RegionCountBean next = iterator.next();
                int pCount = 0; double pRate = 0d; int cCount = 0;
                if(next.getRegionEn().compareToIgnoreCase("Guangdong")==0){
                    pCount = 8; pRate = 8.0d/17.0d;
                    cCount = 8;
                }
                if(next.getRegionEn().compareToIgnoreCase("Shanghai")==0){
                    pCount = 5; pRate = 5.0d/17.0d;
                    cCount = 5;
                }
                if(next.getRegionEn().compareToIgnoreCase("Jiangsu")==0){
                    pCount = 4; pRate = 4.0d/17.0d;
                    cCount = 4;
                }
                next.setCount(pCount);
                next.setRate(pRate);
                CityCountBean cityCountBean = next.getCityCounts().get(0);
                cityCountBean.setCount(cCount);
            }
        }
        return chinaRegionList;
    }
}
